// 通过标志位来终止线程
public class ThreadStop implements Runnable{

    private boolean flag = true;
    @Override
    public void run() {
        int i=0;
        while(flag){
            System.out.println("run...thread "+(i++));
        }
    }
    // 使用标志位的stop()方法
    public void stop(){
        flag = false;
    }

    public static void main(String[] args) {
        ThreadStop threadStop = new ThreadStop();
        new Thread(threadStop).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("main...thread "+i);
            if(i==800){
                threadStop.stop();
                System.out.println("run thread should stop.");
            }
        }
    }
}
